package billrestauration.controllers;

 
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

 

import org.springframework.web.multipart.MultipartFile;

import billrestaurant.extra.upload.FileUpload;
import billrestauration.daos.MainDao;
import billrestauration.entities.Menu;
import billrestauration.util.Util;
/**
 * 
 * @author billy
 *
 */
/**
 * 
 * JAVA_OPTS="-Dfile.encoding=UTF-8 -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC"
 
 *
 */
@Controller
public class MainController {
	@Autowired(required=true)
	private MainDao dao;
	@Autowired (required=true) 
	billrestauration.extra.validation.BillRestaurationValidator fileValidator;  
	 
	@Value("${upload.path}")
	  String path;
	@RequestMapping(value="/",method=RequestMethod.GET)
public String index(Model model){
		//System.out.println( (Util.currentDir())+"\n"+Util.exec(new String[]{"pwd","which java","whoami","ls"}));

		model.addAttribute("menudujour", dao.listeMenuDuJour() );
	return "index";
}
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String index2(Model model){
		return index(model);
	}
	@RequestMapping(value="/admin",method=RequestMethod.GET)
	public String admin(Model model){
		return "admin";
	}
	@RequestMapping(value="/admin/ajoutmenu",method=RequestMethod.GET)
	public String ajoutmenu(Model model,@ModelAttribute("menu") Menu menu ){
		if(!model.containsAttribute("menu"))
		model.addAttribute("menu", new Menu());
		return "ajoutmenu";
	}
	@RequestMapping(value="/admin/ajoutmenu",method=RequestMethod.POST)
	  
	public String ajoutmenuaction(Model model,@ModelAttribute("menu") Menu menu,BindingResult result,Locale locale){
		 
		try{
		 fileValidator.validate( menu, result,locale); 
		 
		model.addAttribute("menu",menu);
		   if (result.hasErrors()) {  
			 //model.addAttribute("erreurajoutmenu", result.getAllErrors().toString()) ;
			 return ajoutmenu(model,menu );
			  }  
		
		/*if(upload!=null){
			String pat=upload.move(path);
			menu.setImage("/rawresources/"+pat);
		}*/
	//	System.out.println(request.getUserPrincipal());
		dao.addMenu(menu);
		model.addAttribute("erreuraoutmenu", "menu ajouté avec succès");
		}catch(Exception e){
			model.addAttribute("erreurajoutmenu", e.getMessage());
			e.printStackTrace();
		}
		System.out.println(menu);
		return "ajoutmenu";
	}
	@RequestMapping(value="/admin/listemenus",method=RequestMethod.GET)
	public String listemenus(Model model){
		model.addAttribute("erreur",null);
		model.addAttribute("listemenus",dao.listeMenu());
		return "listemenus";
	}
	 
	@RequestMapping(value="/admin/ajouterillustrationaumenu/{id}",method=RequestMethod.GET)
	public String ajouterillustrationaumenu(Model model,@PathVariable(value="id") Long id,
			@ModelAttribute("uploadedFile") FileUpload uploadedFile, BindingResult result){
		
		 
			 model.addAttribute("id",id); 
		return "ajouterillustrationaumenu";
	}
	@RequestMapping(value="/admin/ajouterillustrationaumenu/{id}",method=RequestMethod.POST)
	public String ajouterillustrationaumenuAction(Model model,@PathVariable(value="id") Long id,
			@ModelAttribute("uploadedFile") FileUpload uploadedFile, BindingResult result){
		 MultipartFile file =null;
		 String url=null;
		try{
			file = uploadedFile.getFile(); 
		  fileValidator.validate(uploadedFile, result);  
		  if (result.hasErrors()) {  
			return   ajouterillustrationaumenu(  model, id,
						 uploadedFile,   result);
					
			  }  
		}catch(Exception e){
			model.addAttribute("erreurajoutillustration", e.getMessage());
			return   ajouterillustrationaumenu(  model, id,
					 uploadedFile,   result);
		
		}
		  try {
			url="/static/resources/"+uploadedFile.move(Util.currentDir() );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			model.addAttribute("erreurajoutillustration", e.getMessage());
			return   ajouterillustrationaumenu(  model, id,
					 uploadedFile,   result);
		
		}
		  
		  /**
		   * recuperation du menu
		   */
		  Menu m=dao.findMenuById(id);
		  if(null==m){
			  model.addAttribute("erreurajoutillustration", "le menu d'id["+id+"] n'existe pas!!!");
				return   ajouterillustrationaumenu(  model, id,
						 uploadedFile,   result);
			
		  }
		  m.setImage(url);
		  dao.addMenu(m);
		  System.out.println(m);
		  return  "admin";  
		 
	}
	
	@RequestMapping(value="/admin/modifiermenu/{id}",method=RequestMethod.GET)
	public String modifiemenu(Model model,@PathVariable(value="id") Long id){
		
		 Menu m=dao.findMenuById(id);
		 if(null==m)
			 model.addAttribute("menu",new Menu());
		 else
			 model.addAttribute("menu",m); 
		return ajoutmenu(model, m);
	}
	@RequestMapping(value="/admin/supprimermenu/{id}",method=RequestMethod.GET)
	public String supprimermenu(Model model,@PathVariable(value="id") Long id){
		
		 Menu m=dao.findMenuById(id);
		 if(null==m){
			 model.addAttribute("erreur","le menu d'id["+id+"] n'exite pas!!");
			 return listemenus(model);
		 }
		 try{
			 if(dao.supprimerMenu(m))
				 model.addAttribute("erreur","le menu d'id["+id+"] a été supprimé avec succès");
				
		 }catch(Exception e){
			 model.addAttribute("erreur",e.getMessage());
			 return listemenus(model);
				
		 }
		 
		 return listemenus(model);
			 
	}
}
